<template>
	<view>
		<view class="cu-bar bg-white ">
			<view class="action">
				图片
			</view>
			<view class="action">
				{{imgList.length}}/{{size}}
			</view>
		</view>
		<view class="cu-form-group">
			<view class="grid col-4 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
				 <image :src="imgList[index]" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-deletefill'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<size">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>
		
		
	</view>
</template>

<script>
	import upload from "@/wx-cos/upload.js";
	export default {
		props: {
			size: {
				type: Number,
				default: 1
			},
			idKey: {
				type: String,
				default: ""
			},
			value: {
				type: Array,
				default: function() {
					return [];
				}
			},
		},
		data: function() {
			return {
				imgList: []
			};
		},
		mounted: function(){
			for(let index in this.value) {
				this.imgList.push(this.value[index]);
			}
		},
		methods: {
			ChooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						let tempPath = res.tempFilePaths[0]
						upload.checkUploadFile(
							tempPath, 
							new Date().getTime(), 
							"imgs/"+this.idKey, 
							pro => {
								
							}).then(data => {
							this.imgList.push(data.url);
							this.$emit("input", this.imgList);
						});
						
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '删除确认',
					content: '确定要删除图片吗？',
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1);
							this.$emit("input", this.imgList);
						}
					}
				})
			},
		}
	}
</script>

<style>
</style>